<?php

namespace App\Service\Message\v1_2_8;

use App\Model\Gift;
use App\Model\UserPackBag;
use App\Model\UserProfile;
use App\Service\BaseService;

/**
 * 礼物列表
 */
class GiftListService extends BaseService
{
    public function handle()
    {
        $userId = $this->getUserId();
        //礼物数量
        $ret['gift_num'] = [
            ['num' => 1, 'text' => '一心一意'],
            ['num' => 10, 'text' => '十全十美'],
            ['num' => 99, 'text' => '长长久久'],
            ['num' => 520, 'text' => '我爱你'],
            ['num' => 1314, 'text' => '一生一世'],
        ];
        //礼物列表
        $ret['gift_list'] = [];
        //女用户礼物列表展示
        if (UserProfile::checkIsHost($userId)) {
            $giftIds = explode(',', env('HOST_SHOW_GIFTID', '62'));
            if ($giftIds) {
                $data = Gift::getBatchInfo($giftIds);
                foreach ($data as $item) {
                    $ret['gift_list'][] = Gift::buildItem($item);
                }
            }
        }

        //常规礼物
        $data = Gift::getList();
        foreach ($data as $item) {
            if (in_array($item['id'], explode(',', env('NOT_SHOW_GIFTID', '84')))) continue;
            $ret['gift_list'][] = Gift::buildItem($item);
        }

        //背包礼物
        $ret['bag_list'] = [];
        $data = UserPackBag::getList($userId);
        $giftIds = array_column($data, 'gift_id');
        $giftInfo = Gift::getBatchInfo($giftIds);
        foreach ($data as $item) {
            if (!isset($giftInfo[$item['gift_id']])) continue;
            $bagItem = Gift::buildItem($giftInfo[$item['gift_id']]);
            $bagItem['num'] = $item['ctn'];
            $ret['bag_list'][] = $bagItem;
        }
        check_dev_env() && log_user_result($userId, $ret['gift_list']);
        return $ret;
    }
}
